package ai.peanut.selfbms.controller.sys;


import ai.peanut.selfbase.common.domain.DataResult;
import ai.peanut.selfbase.common.enums.ResultTypeEnum;
import ai.peanut.selfbase.common.utils.UserUtils;
import ai.peanut.selfbase.modules.role.entity.Role;
import ai.peanut.selfbase.modules.role.pojo.RoleParams;
import ai.peanut.selfbase.modules.role.service.IRoleService;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>z
 * 角色表 前端控制器
 * </p>
 *
 * @author lichong
 * @since 2019-02-16
 */
@Controller
@Slf4j
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService iRoleService;
    @RequiresPermissions("role:list")
    @RequestMapping("/list")
    @ResponseBody
    public DataResult<Page> list(RoleParams params, HttpServletRequest request, HttpServletResponse response) {
        try {
            Map map = new HashMap<>();
            log.info("角色 管理后台 :{}", "请求参数:" + params.toString());

            Page page = iRoleService.selectAllPageByParams(params);
//            map.put("records", page.getRecords() ) ;
//            map.put("total", page.getTotal() ) ;
            return DataResult.success(page, Page.class);
        } catch (Exception e) {
            log.error("角色 管理后台 列表异常:", e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }
    @RequiresPermissions("role:info")
    @RequestMapping("/info")
    @ResponseBody
    public DataResult<Role> form(Long id) {
        try {
            log.info("获取cps商品详情 管理后台 :{}", "请求id:" + id);
            Role content = iRoleService.selectById(id);
            return DataResult.success(content, Role.class);
        } catch (Exception e) {
            log.error("获取cps商品详情 管理后台 :", e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }

    @RequiresPermissions("role:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public DataResult<Boolean> saveOrUpdate(@RequestBody RoleParams params, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (params.getId() == null) {
                return iRoleService.insertRole(params);
            } else {
                return iRoleService.updateRole(params);
            }
        } catch (Exception e) {
            log.error("cps商品修改 管理后台 :", e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }
    @RequiresPermissions("role:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public DataResult<Boolean> delete(@RequestBody RoleParams params) {
        try {
            log.info("cps商品删除 管理后台 :{}", "请求参数:" + params.toString());
            params.setUpdateUserId(UserUtils.getCurrentUser().getId());
            params.setDelFlag(0);
            int deNum = iRoleService.deleteByIds(params);
            return DataResult.success();
        } catch (Exception e) {
            log.error("cps商品删除 管理后台 :", e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());

    }
    @RequiresPermissions("role:allList")
    @RequestMapping("/allList")
    @ResponseBody
    public DataResult<List<Role>> list( HttpServletRequest request, HttpServletResponse response) {
        try {
            RoleParams params = new RoleParams () ;
            params.setDelFlag(1) ;
            List<Role> list = iRoleService.selectAllByParams(params);
            return DataResult.success(list, (Class<List<Role>>)list.getClass());
        } catch (Exception e) {
            log.error("角色 管理后台 列表异常:", e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }
}
